var api = {};
var check = {};
import {
  http,
  domain
} from '../../../utils/request'

function omit(obj, props) {
  const result = {};
  for (const key in obj) {
    if (props.indexOf(key) === -1) {
      result[key] = obj[key];
    }
  }
  return result;
}

var app = getApp();
Page({
  data: {
    uploadFiles: [],
    uploadFilesAvatar: [],
    uploadFilesFront: [],
    uploadFilesBackend: [],
    userType: 0,
    isPrivate: 1,
    mobile: '',
    password: '',
    confirmPassword: '',
    identityCard: '',
    nickName: '',
    contact: '',
    contactMobile: '',
    email: '',
    gridConfig: {
      width: 218,
      height: 218,
      column: 1,
    },
    imageProps: {
      mode: 'aspectFit',
    },
    uploadFiles: '',
    userName: '',
    cityVisible: false,
    userTypeText: '',
    types: [{
        label: '客户',
        value: 0
      },
      {
        label: '供应商',
        value: 1
      },
    ]
  },

  async handleSuccessAvatar(e) {
    wx.uploadFile({
      url: `${domain}/upload`,
      filePath: e.detail.files[0].url,
      name: 'file',
      formData: {
        file: e.detail.files[0].url
      },
      success: res => {
        this.setData({
          uploadFilesAvatar: [{
            name: 'image',
            url: `${JSON.parse(res.data).data.imgUrl}`
          }]
        })
      }
    })
  },

  async handleSuccessFront(e) {
    wx.uploadFile({
      url: `${domain}/upload`,
      filePath: e.detail.files[0].url,
      name: 'file',
      formData: {
        file: e.detail.files[0].url
      },
      success: res => {
        this.setData({
          uploadFilesFront: [{
            name: 'image',
            url: `${JSON.parse(res.data).data.imgUrl}`
          }]
        })
      }
    })
  },

  async handleSuccessBackend(e) {
    wx.uploadFile({
      url: `${domain}/upload`,
      filePath: e.detail.files[0].url,
      name: 'file',
      formData: {
        file: e.detail.files[0].url
      },
      success: res => {
        this.setData({
          uploadFilesBackend: [{
            name: 'image',
            url: `${JSON.parse(res.data).data.imgUrl}`
          }]
        })
      }
    })
  },

  async handleSuccess(e) {
    wx.uploadFile({
      url: `${domain}/upload`,
      filePath: e.detail.files[0].url,
      name: 'file',
      formData: {
        file: e.detail.files[0].url
      },
      success: res => {
        this.setData({
          uploadFiles: [{
            name: 'image',
            url: `${JSON.parse(res.data).data.imgUrl}`
          }]
        })
      },
    })
  },

  async startRegister() {
    var that = this;

    // if (this.data.password.length < 6 || this.data.userName.length < 6) {
    //   wx.showModal({
    //     title: '错误信息',
    //     content: '用户名和密码不得少于6位',
    //     showCancel: false
    //   });
    //   return false;
    // }

    if (this.data.email.length <= 0) {
      wx.showModal({
        title: '错误信息',
        content: '邮箱不得为空',
        showCancel: false
      });
      return false;
    }

    if (this.data.userType === 1 && !this.data.nickName) {
      wx.showModal({
        title: '错误信息',
        content: '企业名称不得为空',
        showCancel: false
      });
      return false;
    }

    if (this.data.userType === 1 && !this.data.creditCode) {
      wx.showModal({
        title: '错误信息',
        content: '统一社会信用码不得为空',
        showCancel: false
      });
      return false;
    }

    if (this.data.password != this.data.confirmPassword) {
      wx.showModal({
        title: '错误信息',
        content: '确认密码不一致',
        showCancel: false
      });
      return false;
    }

    if (this.data.password?.length < 9) {
      wx.showModal({
        title: '密码长度至少九位',
        content: '密码需要密码长度至少九位',
        showCancel: false
      });
      return false;
    }

    if (!/^(?=.*[0-9])(?=.*[a-zA-Z])(?=.*[\W_]).{8,}$/.test(this.data.password)) {
      wx.showModal({
        title: '密码保密级别太低',
        content: '密码需要包含字母、数字、符号',
        showCancel: false
      });
      return false;
    }

    if (!/^(?:(?:\+|00)86)?1(?:(?:3[\d])|(?:4[5-79])|(?:5[0-35-9])|(?:6[5-7])|(?:7[0-8])|(?:8[\d])|(?:9[189]))\d{8}$/.test(this.data.contactMobile)) {
      wx.showModal({
        title: '手机号码格式错误',
        content: '请输入正确格式手机号',
        showCancel: false
      });
      return false;
    }

    const imgUrl = this.data.uploadFilesAvatar?.[0]?.url
    const licenseUrl = this.data.uploadFiles?.[0]?.url
    const imgUrlUp = this.data.uploadFilesFront?.[0]?.url
    const imgUrlDown = this.data.uploadFilesBackend?.[0]?.url

    const data = await http('POST', '/user/register', {
      ...omit(this.data, ['types', 'cityVisible', 'gridConfig', 'imageProps', 'uploadFilesAvatar', 'uploadFiles', 'uploadFilesFront', 'uploadFilesBackend']),
      imgUrl,
      licenseUrl,
      imgUrlUp,
      imgUrlDown,
      mobile: this.data.contactMobile,
    })
    wx.showToast({
      title: '注册成功！',
    })
    setTimeout(() => {
      wx.navigateBack()
    }, 1000)
  },

  radioChange(e) {
    const userType = e.detail.value
    this.setData({
      userType,
    })
    if (userType === 1) {
      this.setData({
        isPrivate: 1,
      })
    }
  },

  privateRadioChange(e) {
    this.setData({
      isPrivate: e.detail.value
    })
  },

  onPickerChange(e) {
    const {
      value,
      label
    } = e.detail;

    this.setData({
      userTypeText: label[0],
      userType: value[0],
    });
  },

  onPickerCancel(e) {
    const {
      key
    } = e.currentTarget.dataset;
    this.setData({
      [`${key}Visible`]: false,
    });
  },

  onCityPicker() {
    this.setData({
      cityVisible: true
    });
  },

  handleRemove(event) {
    const type = event.target.dataset.type
    this.setData({
      [`uploadFiles${type}`]: []
    })
  }
})